﻿using Guru.Framework.Extensions;
using System;
using System.Windows.Forms;
using Telerik.WinControls.UI;

namespace CRMG.Container.UserControl
{
    public class DatePanel : RadPanel
    {
        protected readonly int[] MonthDays = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        protected readonly int _height = 35;

        protected TableLayoutPanel tblLytMain;
        protected RadLabel lblYear, lblMonth, lblDay;
        protected ComboBox cmbxYear, cmbxMonth, cmbxDay;

        private DateTime _dt;
        int year;
        int month;
        int day;
        //private string _dateCulture = "tr-TR";
        public DateChangedEventHandler DateChange;

        public DatePanel()
            : base()
        {
            DateTime dt = DateTime.Now;
            year = dt.Year;
            month = dt.Month;
            day = dt.Day;
            InitComp();
            LoadInteger2Combo(cmbxYear, DateTime.MinValue.Year, DateTime.MaxValue.Year);
            LoadInteger2Combo(cmbxMonth, 1, 12);
            LoadInteger2Combo(cmbxDay, 1, 31);
            cmbxYear.SelectedItem = year.ToString();
            cmbxMonth.SelectedItem = month.ToString();
            cmbxDay.SelectedItem = day.ToString();
            cmbxDay.SelectedIndexChanged += new EventHandler(this.DayChange);
            cmbxMonth.SelectedIndexChanged += new EventHandler(this.MonthChange);
            cmbxYear.SelectedIndexChanged += new EventHandler(this.YearChange);
            this.Height = _height;
        }

        protected void InitComp()
        {
            this.SuspendLayout();
            this.Font = new System.Drawing.Font("Segoe UI", 10.0f, System.Drawing.FontStyle.Regular);
            this.Text = string.Empty;
            this.tblLytMain = new TableLayoutPanel();
            this.Anchor = AnchorStyles.None;
            this.tblLytMain.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 17.00F));
            this.tblLytMain.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 19.00F));
            this.tblLytMain.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 18.00F));
            this.tblLytMain.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 14.0F));
            this.tblLytMain.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 18.00F));
            this.tblLytMain.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 14.0F));

            this.tblLytMain.RowStyles.Add(new RowStyle(SizeType.Absolute, 35.0F));


            this.lblYear = new RadLabel();
            this.lblYear.Text = "Year :";
            this.lblYear.Font = this.Font;
            this.lblYear.Dock = DockStyle.Fill;
            tblLytMain.Controls.Add(this.lblYear, 0, 0);


            this.cmbxYear = new ComboBox();
            this.cmbxYear.DropDownStyle = ComboBoxStyle.DropDownList;
            this.cmbxYear.Font = this.Font;
            this.cmbxYear.Dock = DockStyle.Fill;
            this.tblLytMain.Controls.Add(this.cmbxYear, 1, 0);

            this.lblMonth = new RadLabel();
            this.lblMonth.Text = "Month :";
            this.lblMonth.Font = this.Font;
            this.lblMonth.Dock = DockStyle.Fill;
            this.tblLytMain.Controls.Add(this.lblMonth, 2, 0);

            this.cmbxMonth = new ComboBox();
            this.cmbxMonth.DropDownStyle = ComboBoxStyle.DropDownList;
            this.cmbxMonth.Font = this.Font;
            this.cmbxMonth.Dock = DockStyle.Fill;
            this.tblLytMain.Controls.Add(this.cmbxMonth, 3, 0);

            this.lblDay = new RadLabel();
            this.lblDay.Text = "Day :";
            this.lblDay.Font = this.Font;
            this.lblDay.Dock = DockStyle.Fill;
            this.tblLytMain.Controls.Add(this.lblDay, 4, 0);

            this.cmbxDay = new ComboBox();
            this.cmbxDay.DropDownStyle = ComboBoxStyle.DropDownList;
            this.cmbxDay.Font = this.Font;
            this.cmbxDay.Dock = DockStyle.Fill;
            this.tblLytMain.Controls.Add(this.cmbxDay, 5, 0);

            this.Controls.Add(this.tblLytMain);
            this.tblLytMain.Dock = DockStyle.Fill;
            this.Size = new System.Drawing.Size(this.tblLytMain.Width, this.tblLytMain.Height);
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        public string YearString
        {
            get { return lblYear.Text; }
            set { lblYear.Text = value; }
        }

        public string MonthString
        {
            get { return lblMonth.Text; }
            set { lblMonth.Text = value; }
        }

        public string DayString
        {
            get { return lblDay.Text; }
            set { lblDay.Text = value; }
        }

        public DateTime Date
        {
            get
            { return new DateTime(year, month, day, 0, 0, 0); }
            internal set
            {
                _dt = value;
                if (DateChange != null)
                {
                    DateChange(this, new DateChangedEventArgs(_dt));
                }
            }
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            this.Height = _height;
        }

        protected void LoadInteger2Combo(ComboBox cmbx, int startVal, int endVal)
        {
            cmbx.Items.Clear();
            for (int i = startVal; i <= endVal; i++)
            {
                cmbx.Items.Add(i.ToString());
            }
        }

        protected void setDays(int year, int month)
        {
            try
            {
                LoadInteger2Combo(cmbxDay, 1, GetMonthDays(year, month));
                cmbxDay.SelectedIndex = 0;
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void YearChange(object sender, EventArgs e)
        {
            try
            {
                setDays(cmbxYear.SelectedItem.ToInt(), cmbxMonth.SelectedItem.ToInt());
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void MonthChange(object sender, EventArgs e)
        {
            try
            {
                setDays(cmbxYear.SelectedItem.ToInt(), cmbxMonth.SelectedItem.ToInt());
            }
            catch (Exception)
            {
                throw;
            }
        }


        protected void DayChange(object sender, EventArgs e)
        {
            try
            {
                Date = GetTime();
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected int GetMonthDays(int year, int month)
        {
            if (year % 4 == 0 && month == 2)
                return 29;
            else
                return MonthDays[month - 1];
        }

        protected DateTime GetTime()
        {
            try
            {
                int year = cmbxYear.SelectedItem.ToInt();
                int month = cmbxMonth.SelectedItem.ToInt();
                int day = cmbxDay.SelectedItem.ToInt();
                return new DateTime(year, month, day);
            }
            catch (Exception)
            {
                throw;
            }
        }

    }

    public delegate void DateChangedEventHandler(object sender, DateChangedEventArgs de);

    public class DateChangedEventArgs : EventArgs
    {
        private DateTime _dt;
        public DateTime Date { get { return _dt; } }

        public DateChangedEventArgs(DateTime datetime)
        {
            _dt = datetime;
        }
    }
}
